package com.bop.dehong.base;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;

import com.base.dehong.base.FragmentBaseFrame;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.util.LoadingAnimation;
import com.umeng.analytics.MobclickAgent;

@SuppressLint("NewApi")
public abstract class BaseFragment extends FragmentBaseFrame implements OnClickListener,OnItemClickListener {
	protected String mPageName;
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		mPageName = getClass().getName();
		initTop();
		initUI();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mContentView = inflater.inflate(getContentViewId(), null);
		return mContentView;
	}
	LoadingAnimation an;
	@Override
	protected void showProgressDialog() {
		if (an == null) {
			an = new LoadingAnimation(getActivity());
		}
		an.createAnimation();
	}

	@Override
	public void disappearProgressDialog() {
		if (an!= null) {
			an.dismissAnimation();
		}
	}
	/**
	 * 初始化标题,最新处理
	 */
	protected abstract void initTop();
	/**
	 * 初始化UI，，第二处理
	 */
	protected abstract void initUI();
	/**
	 * 初始化数据，，第三处理
	 */
	public abstract void initData();
	/**
	 * 设置ContentView
	 * @return
	 */
	protected abstract int getContentViewId();

	/**
	 * 通过类名启动Activity
	 * 
	 * @param pClass
	 */
	protected void openActivity(Class<?> pClass) {
		openActivity(pClass, null);
	}

	/**
	 * 通过类名启动Activity，并且含有Bundle数据
	 * 
	 * @param pClass
	 * @param pBundle
	 */
	protected void openActivity(Class<?> pClass, Bundle pBundle) {
		Intent intent = new Intent(this.getActivity(), pClass);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		startActivity(intent);
	}

	/**
	 * 通过Action启动Activity
	 * 
	 * @param pAction
	 */
	protected void openActivity(String pAction) {
		openActivity(pAction, null);
	}

	/**
	 * 通过Action启动Activity，并且含有Bundle数据
	 * 
	 * @param pAction
	 * @param pBundle
	 */
	protected void openActivity(String pAction, Bundle pBundle) {
		Intent intent = new Intent(pAction);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		startActivity(intent);
	}
	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(mPageName);
		MobclickAgent.onPause(getActivity());
		ToastUtils.hideToast();
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onPageStart(mPageName);
		MobclickAgent.onResume(getActivity());
	}
	
	public void registerViewClickListener(View... views) {
		for (int i = 0; i < views.length; i++) {
			views[i].setOnClickListener(this);
		}
	}
}
